# ------------------------------------------------------------------------------------------------
### Generate Figure 13: Effects by government unit passing the resolution
# ------------------------------------------------------------------------------------------------

# Load data
load("../data/processed/LGBTQ_election_data.Rda")

# Define level labels for better readability
level_labels <- c(
  "any_level_any_type" = "Any Level",
  "province_any_type" = "Only Province Level", 
  "county_any_type" = "Only County Level",
  "municipality_any_type" = "Only Municipality Level"
)

# Load and process municipality boundaries
PL_gmina <- gisco_get_lau(country = "PL", year="2020") %>%
  mutate(id = paste0(substr(LAU_ID, 5, 6), substr(LAU_ID, 10, nchar(LAU_ID)))) %>%
  left_join(
    LGBTQ_election_data %>%
      select(id, any_level_any_type, municipality_any_type, county_any_type, province_any_type) %>% 
      unique(), 
    by = "id"
  )
 
# Prepare data for plotting
plot_data <- PL_gmina %>%
  gather("level", "treat", any_level_any_type:province_any_type) %>%
  mutate(
    level = factor(level, 
                   levels = names(level_labels), 
                   labels = level_labels)
  )

# Generate plot
ggplot(plot_data) +
  geom_sf(aes(fill = as.factor(treat))) +
  scale_fill_manual(
    values = c("transparent", "#D9534F"), 
    labels = c("No resolution", "Anti-LGBTQ+ resolution")
  ) +
  theme_map() +
  facet_wrap(~level) +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    legend.position = "bottom",
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    legend.justification = c(.5),
    legend.title = element_blank(),
    text = element_text(size = 23)
  ) 

# Save the plot
ggsave("../output/plots/figure13.pdf", width = 14, height = 14, dpi = 700)

